﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace CECB
{
    /// <summary>
    /// Interaction logic for ReportManager_Window.xaml
    /// </summary>
    public partial class ReportManager_Window : Window
    {
        public ReportManager_Window()
        {
            InitializeComponent();
            ProjectAutocompleteTextBox.ItemsSource = new cecbData.DSSEntities().Projects;
          
        }


        private void LoadReport1(String project)
        {


            ReportDocument report = new ReportDocument();
            report.Load("../../Reports/CrystalReport2.rpt");

            using (cecbData.DSSEntities db = new cecbData.DSSEntities())
            {

                //report.SetDataSource(from s in db.SimpleMatrixValues join m in db.Impacts on s.impt_reference equals m.impt_reference
                //                         join a in db.Activities on s.actv_reference equals a.actv_reference
                //                         join c in db.Components on m.impt_component equals c.cmpt_reference
                //                         where s.proj_reference == project
                //                         select new
                //                         {
                //                             s.proj_reference, 
                //                              s.impt_reference,

                //                            s.mtrx_value,
                //                             m.impt_name,
                //                             a.actv_phase,
                //                             a.actv_name,
                //                             a.actv_reference



                //                         });


                //====================================================
                //  select proj_reference , mtrx_value from simplematrixvalues
                // select proj_reference , mtrx_magnitude from leopoldmatrixvalues

                //  report.SetDataSource(from c in db.Members select new { c.mem_name,c.mem_id });
                //report.SetDataSource(from c in db.SimpleMatrixValues select new { c.proj_reference,c.mtrx_value });
                //report.SetDataSource(db.ReportSimple("PROJ1"));




            }
            //   report.SetParameterValue("pro", "PROJ1");
            // report.SetParameterValue("param2", "FUCK you");
            // report.SetParameterValue("param3", "Fdfsdf");
            crystalReportsViewer1.ViewerCore.ReportSource = report;
        }

       
        SqlConnection conn = null;

        public void LoadReportSQlConnection(String proj)
        
        {

            cecbData.Projects pro = (cecbData.Projects)ProjectAutocompleteTextBox.SelectedItem;
             
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CECB.Properties.Settings.DSSConnectionString"].ConnectionString);


            this.Cursor = Cursors.Wait;
            
            System.Data.SqlClient.SqlDataAdapter da;

            try
            {

                ReportDocument report = new ReportDocument();
                report.Load("../../Reports/CrystalReport7.rpt");


                try
                {
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                     
                        SqlCommand cmd = new SqlCommand();
                        System.Data.DataSet ds = new System.Data.DataSet();
                        da = new System.Data.SqlClient.SqlDataAdapter("ReportSimpleMatrix", conn);
                        da.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
                        da.SelectCommand.Parameters.AddWithValue("@Proj",  proj);
                       
                        da.Fill(ds, "ReportSimpleMatrix");
                        
                        report.Database.Tables[0].SetDataSource(ds.Tables[0]);
                        report.SetDataSource(ds);
                      //  report.SetParameterValue("@Pname", pro.proj_name.ToString());
                        crystalReportsViewer1.ViewerCore.ReportSource = report;
                     
                }
                catch (Exception ex)
                {

                    MessageBox.Show("System Error In Report!  " + ex, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }





            }
            catch (SqlException)
            {

                MessageBox.Show("Try to connect to internet first!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            this.Cursor = Cursors.Arrow;



        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void ReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                LoadReportSQlConnection(ProjectAutocompleteTextBox.Text);
            }
            catch (Exception)
            {
                
                 
            }
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }




    }
}
